package com.shirley.customcontrols;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shirley.android.tabbutton.TabButtonItemBean;
import com.shirley.android.tabbutton.TabButtonRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 主界面的Tab3对应的fragment
 * Created by ZLJ on 2017/12/18.
 */
public class Fragment3 extends Fragment {

    @BindView(R.id.tab_button)
    TabButtonRecyclerView tabButton;
    Unbinder unbinder;

    private Fragment mCurFragment;
    private Fragment[] mFragments = {new BreadCrumbFragment(), new ButtonGroupFragment()};

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment3, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tabButton.setData(initData());
        tabButton.setOnItemViewClickListener(new TabButtonRecyclerView.OnItemViewClickListener() {
            @Override
            public void onItemViewClick(int position, TabButtonItemBean data) {
                // 切换Fragment
                switchFragment(mFragments[position]);
            }
        });

        // 加载WordGroupOrderFragment
        switchFragment(mFragments[0]);
    }

    public void switchFragment(Fragment to) {
        FragmentTransaction transaction = getActivity().getSupportFragmentManager()
                .beginTransaction().setCustomAnimations(android.R.anim.fade_in,
                        android.R.anim.fade_out);
        if (mCurFragment == null){
            transaction.add(R.id.fl_container3, to).commit(); // 首次add第一个Fragment到Activity中
        } else {
            if (mCurFragment != to) {
                if (!to.isAdded()) {	// 先判断是否被add过
                    transaction.hide(mCurFragment).add(R.id.fl_container3, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
                } else {
                    transaction.hide(mCurFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个
                }
            }
        }
        mCurFragment = to;
    }

    public List<TabButtonItemBean> initData() {
        List<TabButtonItemBean> list = new ArrayList<>();

        TabButtonItemBean bean1 = new TabButtonItemBean();
        bean1.setCode(0);
        bean1.setText("BreadCrumb");
        list.add(bean1);

        TabButtonItemBean bean2 = new TabButtonItemBean();
        bean2.setCode(1);
        bean2.setText("ButtonGroup");
        list.add(bean2);

        return list;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
